package com.dikang.yygh.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dikang.yygh.feign.DictFeignClient;
import com.dikang.yygh.mapper.HospitalMapper;
import com.dikang.yygh.model.hosp.Hospital;
import com.dikang.yygh.repository.HospitalRepository;
import com.dikang.yygh.service.HospitalService;
import com.dikang.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import sun.rmi.transport.ObjectTable;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl extends ServiceImpl<HospitalMapper, Hospital> implements HospitalService {
    @Autowired
    private HospitalRepository hospitalRepository;
    @Autowired
    private DictFeignClient dictFeignClient;
    @Override
    public void save(Map<String, Object> paramrMap) {
        //先转成Hospital对象
        String jsonString = JSONObject.toJSONString(paramrMap);
        Hospital hospital = JSONObject.parseObject(jsonString, Hospital.class);

        // 判断是否存在该数据

        Hospital hospitalExit = hospitalRepository.getHospitalByHoscode(hospital.getHoscode());
        // 如果存在更新
        if (hospitalExit != null){
            hospital.setId(hospitalExit.getId());
            hospital.setStatus(hospitalExit.getStatus());
            hospital.setUpdateTime(new Date());
            hospital.setCreateTime(hospitalExit.getCreateTime());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }else{
            // 如果不存在就修改
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }

    }

    @Override
    public Hospital getHospitalByHosCode(String hoscode) {
        return hospitalRepository.getHospitalByHoscode(hoscode);
    }

    @Override
    public Page selectPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        Sort sort = Sort.by(Sort.DEFAULT_DIRECTION, "hoscode");
        Pageable pageable = PageRequest.of(page-1,limit,sort);
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        hospital.setIsDeleted(0);
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Hospital> example = Example.of(hospital, matcher);
        Page<Hospital> hospitalPage = hospitalRepository.findAll(example, pageable);
        hospitalPage.getContent().stream().forEach(item ->{
            this.setHospitalHosType(item);
        });
        return hospitalPage;
    }

    @Override
    public void updateStatus(String id, Integer status) {
       if (status == 0 || status ==1){
           Hospital hospital = hospitalRepository.findById(id).get();
           hospital.setStatus(status);
           hospital.setUpdateTime(new Date());
           hospitalRepository.save(hospital);

       }
    }
    // 查询详情
    @Override
    public Map<String, Object> show(String id) {
        // 这里将医院简介和预订信息分开装
        Hospital hospital = this.setHospitalHosType(hospitalRepository.findById(id).get());
        Map<String, Object> result = new HashMap<>();
        result.put("hospital",hospital);
        result.put("bookingRule",hospital.getBookingRule());
        hospital.setBookingRule(null);
        return result;
    }

    @Override
    public String getHospName(String hoscode) {
        return  hospitalRepository.getHospitalByHoscode(hoscode).getHosname();
    }

    @Override
    public List<Hospital> getHospitalByHosname(String hosname) {
        List<Hospital> hospitalByHosnameLike = hospitalRepository.getHospitalByHosnameLike(hosname);
        return hospitalByHosnameLike;
    }

    @Override
    public Map<String, Object> item(String hoscode) {
        // 这里将医院简介和预订信息分开装
        Hospital hospital = this.setHospitalHosType(this.getHospitalByHosCode(hoscode));
        Map<String, Object> result = new HashMap<>();
        result.put("hospital",hospital);
        result.put("bookingRule",hospital.getBookingRule());
        hospital.setBookingRule(null);
        return result;
    }

    // 将hospitalHosType中的方法转换成
    public Hospital setHospitalHosType(Hospital hospital) {
        // TODO 这里地方连续4次远程调用，还要查询数据库，这里设想能不能将dict_code+value查出一个map返回给这里根据map去查询出name的值。
        String hosTypeStr = dictFeignClient.getName("Hostype", hospital.getHostype());
       // 查询出省市区
        String province = dictFeignClient.getName(hospital.getProvinceCode());
        String city= dictFeignClient.getName(hospital.getCityCode());
        String districte = dictFeignClient.getName(hospital.getDistrictCode());
        hospital.getParam().put("fullAddress",province+city+districte);
        hospital.getParam().put("hosTypeStr",hosTypeStr);
        return hospital;
    }
}
